package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/3 22:27
 */
public class 合并两个链表1669 {

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {

        ListNode dummy = new ListNode(-1, list1);
        ListNode cur = dummy;
        int index = 0;
        ListNode pre;
        ListNode back;
        // -1 0,1,2,3,4,5
        while (true) {
            if (index == a) {
                pre = cur;
                cur = cur.next;
                while (!(index == b)) {
                    cur = cur.next;
                    index++;
                }
                back = cur.next;
                break;
            }
            index++;
            cur = cur.next;
        }
        pre.next = list2;
        ListNode cur2 = list2;
        while (cur2.next != null) {
            cur2 = cur2.next;
        }
        //节后指针
        cur2.next = back;
        return dummy.next;
    }
}
